package com.minsheng.epidemic_rep.service.impl;

import com.minsheng.epidemic_rep.dao.StudentDao;
import com.minsheng.epidemic_rep.entity.Student;
import com.minsheng.epidemic_rep.service.IStudentService;
import com.minsheng.epidemic_rep.util.GenerateCsv;
import com.minsheng.epidemic_rep.util.ZipUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname StudentServiceImpl
 * @Description TODO
 * @Date 2020/9/27 16:35
 * @Created by You
 */
@Service("studentService")
public class StudentServiceImpl implements IStudentService {
    @Autowired
    private StudentDao studentDao;

    //    @Scheduled(cron = "0 * * * * ?")
    @Override
    public void createCsv(){
        System.out.println("hello!");

        List<String> colName = new ArrayList<String>();
        colName.add("姓名");
        colName.add("年龄");
        colName.add("性别");

//        Student stu = new Student("10000","张三",18,0);
//        System.out.println(stu.toString());
//        Student stu1 = new Student("10001","李四",19,1);

        //从数据库获取学生信息
        List<Student> list = new ArrayList<Student>();
        list = studentDao.findAll();
//        list.add(stu);
//        list.add(stu1);
        System.out.println(list);

        List<List<String>> list1 = new ArrayList<List<String>>();

        for(Student st : list){
            List<String> list2 = new ArrayList<String>();

            if(StringUtils.isBlank(st.getName())){
                list2.add("");
            } else {
                list2.add(String.valueOf(st.getName()));
            }

            if(st.getAge() == null){
                list2.add("");
            } else {
                list2.add(String.valueOf(st.getAge()));
            }

            if(st.getAge() == null){
                list2.add("");
            } else {
                list2.add(String.valueOf(st.getSex()));
            }

            list1.add(list2);

        }

        try {
            GenerateCsv.generateCsv("g:/csv/","测试",colName,list1);
        } catch (IOException e) {
            e.printStackTrace();
        }

//-------------------------------------压缩ZIP文件
        //获取待压缩文件列表(文件具体路径)
        List<String> pathList = ZipUtil.getFile("g:/csv/",0);

        try {
            ZipUtil.zipFiles(pathList,"g:/csv/测试.zip");
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


}
